home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / misc / football / exec / alt_league.rexx next >
OS/2 REXX Batch file  |  1999-11-29  |  19KB  |  568 lines

  1. /* ***********************************************************************
  2.  
  3.    ALTERNATIVE LEAGUE PROGRAM FOR FOOTBALL REXX SUITE
  4.   ----------------------------------------------------
  5.                    Copyright  Mark Naughton 1996
  6.  
  7.  
  8. Version    Date     History
  9. --------------------------------------------------------------------------
  10.  1.0       090996   First release.
  11.            090996   As the sort doesnt flip the negative numbers, was
  12.                     thought to be a problem, the league is checked and
  13.                     flipped correctly (LEAGUE2.REXX)
  14.            120996   Amended same code as it didn't work if the two 'signs'
  15.                     were different. Now flips them correctly.
  16.                     (LEAGUE2.REXX)
  17.            180996   Changed record-count so that it is stored in element
  18.                     zero of each array. Deleted some variable assigns
  19.                     that weren't needed. Amended reading into arrays so
  20.                     that the count is set correctly for one element.
  21.                     Removed all "call "references before library routines.
  22.            180996   Removed all "call " references, used element zero to
  23.                     store the record-count. (LEAGUE2.REXX)
  24.            260996   Amended program so that the sortprogram and the REXX
  25.                     program to display the league are called from here
  26.                     instead of a separate script file. Changed so that the
  27.                     count is not stored in 'zero' element.
  28.            270996   Added code for points_per_draw and points_per_loss.
  29.                     Updated to use new filenames. Improved error messages.
  30.                     Inserted code from LEAGUE2.REXX to speed things up and
  31.                     to tidy the code. Finally got the table to be sorted
  32.                     properly, using a version of VSORTA from asd_library.
  33.  1.1       111196   Added argument so it can become a component of
  34.                     FOOTBALL. Removed GAMEPLAY and EXTRACT. Removed all
  35.                     messages.
  36.            131196   Added checks for files - if not found, exits without
  37.                     a message.
  38.            141196   Added code to highlight the leader.
  39.            201196   Added code to sort out which teams are relegated and
  40.                     then to display the line at the correct place.
  41.            211196   Updated and tidied the display.
  42.            231296   Shortened lines when displaying the league.
  43.            241296   Tidied up display again.
  44.  1.2       110497   Added code to support Points_Per_Goals. Amended the
  45.                     table display.
  46.            070597   Amended code that checks league_file to see if they
  47.                     are in the right order - was using wrong parameters.
  48.            080597   Amended as FOOTSORT is only used for league table file
  49.                     sorting.
  50.  2.0       240997   Tidied up code and added errors in case any files cannot
  51.                     be read or written to. Simplified reading of teams and
  52.                     data (getting it from '.df' now, not '.stats') and
  53.                     getting the total teams without having to re-read the
  54.                     '.stats' file. Added promotion bar.
  55.            151297   Tidied display.
  56.            030599   Fixed bug in relegation bar - it would show it at the
  57.                     maximum position for the number of teams relegated but
  58.                     this wasn't true when a team was relegated and the
  59.                     others still had to fight it out - same for the
  60.                     promotion bar.
  61.            050599   Okay, I decided to change the display again.
  62.  2.1       150599   At request of Jan Andersen, I have modified League.rexx
  63.                     to display home and away stats in the league table.
  64.                     This can be used to replace the standard league program.
  65.            170599   Fixed bug in display of relegated teams as they all
  66.                     showed in upper case when 2 hadn't had the misfortune.
  67.                     Fixed bug reported by Kev (Thanks!) where Bolton from
  68.                     the 97/98 English Premiership weren't upper-cased -
  69.                     traced to the fact that they had the same points as
  70.                     the place above - changed so calculation is changed if
  71.                     its the last game of the season.
  72.            170699   Fixed bug where if all teams had zero points, the
  73.                     promotion bar was still displayed.
  74.            250899   Added error msg to file checks.
  75.            270899   Converted to use locale. Some error messages, before
  76.                     reading the locale, will still be in English.
  77.  
  78.  
  79. **************************************************************************
  80.  
  81. Procedure
  82. ---------
  83.  
  84. 1. Check files exist. Open 'Teams.df'.
  85. 2. Read in teams and their associated data; WIN, DRAW, LOST etc.
  86. 3. Close file. Open 'Teams.stats'. Read data. Close file.
  87. 4. Open 'Teams.sf' for reading.
  88. 5. Read each line that has a game played, and from that get the team names
  89.    and the score. Then update the relevant team data with the result.
  90. 6. Write data in form of a league table to an output file.
  91. 7. Close file.
  92. 8. Call external sortprogram to sort the output file.
  93. 9. Open 'Teams.stats' and read the number of teams, then close.
  94. 10.Open 'League.output' and read contents into two arrays.
  95. 11.Check to see if the teams are in the right order, comparing the points
  96.    of each one with the next and if the goal-differences are in the wrong
  97.    order, then swap the two teams.
  98. 12.Check to see which teams are relegated by calculating the maximum number
  99.    of points available and seeing if the lower teams are able to move up.
  100.    If not they are relegated, according to the number set.
  101. 13.Check to see which teams are promoted by using the same method as above.
  102. 14.Display league and exit.
  103.  
  104. ************************************************************************** */
  105. parse arg league_file
  106.  
  107. version      = 2
  108. league_file  = "Data/" || league_file
  109. input_file   = '.stats'
  110. input2_file  = '.sf'
  111. input3_file  = '.df'
  112. output_file  = 'Data/League.output'
  113. title        = '*LEAGUE_NAME='
  114. points_win   = '*POINTS_PER_WIN='
  115. points_drw   = '*POINTS_PER_DRW='
  116. points_lse   = '*POINTS_PER_LSE='
  117. points_gls   = '*POINTS_PER_GLS='
  118. releg        = '*RELEGATION='
  119. playother    = '*PLAY_OTHER='
  120. promoted     = '*PROMOTED='
  121. team_counter = '*COUNTR='
  122. team         = '*TEAM='
  123. played       = '*PLY='
  124. won          = '*WIN='
  125. drawn        = '*DRW='
  126. lost         = '*LST='
  127. goalsf       = '*GOF='
  128. goalsa       = '*GOA='
  129. points       = '*PTS='
  130. separator    = '*'
  131. teams.       = '???'
  132. teams2.      = '???'
  133. m_ply.       = '???'
  134. m_win.       = '???'
  135. m_drw.       = '???'
  136. m_lost.      = '???'
  137. m_gof.       = '???'
  138. m_goa.       = '???'
  139. m_pts.       = '???'
  140. h_ply.       = '???'
  141. h_win.       = '???'
  142. h_drw.       = '???'
  143. h_lost.      = '???'
  144. h_gof.       = '???'
  145. h_goa.       = '???'
  146. h_pts.       = '???'
  147. a_ply.       = '???'
  148. a_win.       = '???'
  149. a_drw.       = '???'
  150. a_lost.      = '???'
  151. a_gof.       = '???'
  152. a_goa.       = '???'
  153. a_pts.       = '???'
  154. ah_teams.    = '???'
  155. teams_ctr    = 0
  156. playo        = 2
  157. reg          = 2
  158. ptsgls       = 0
  159. ptswin       = 2
  160. ptsdrw       = 1
  161. ptslse       = 0
  162. promo        = 0
  163. not_played   = '__   __'
  164.  
  165.  
  166. if open(datafile,"Data/Football.locale",'r') then do
  167.    line = readln(datafile)
  168.    locdir = strip(line)
  169.    close(datafile)
  170. end
  171. else do
  172.    say
  173.    say "ERROR :    (League)"
  174.    say
  175.    say "Cannot read 'Data/Football.locale' for the locale settings."
  176.    exit
  177. end
  178.  
  179. if open(datafile,"ENV:FootballRXPath",'r') then do
  180.    line = readln(datafile)
  181.    rxdir = strip(line)
  182.    close(datafile)
  183. end
  184. else
  185.    rxdir = "SYS:Rexxc/"
  186.  
  187. locdir = locdir"Exec/Alt_League.data"
  188.  
  189. if exists(locdir) > 0 then do
  190.   address command rxdir'rx 'locdir
  191.   VarCount = getclip('VarCount')
  192.   do i = 1 to VarCount
  193.     interpret getclip('var.'i)
  194.   end
  195. end
  196. else do
  197.    say
  198.    say "ERROR :    (League)"
  199.    say
  200.    say "Cannot find '"locdir"' to read locale settings."
  201.    exit
  202. end
  203.  
  204. if exists(league_file || input_file) = 0  then do
  205.    say
  206.    say al_error
  207.    say
  208.    say al_one"'"league_file||input_file"'."
  209.    exit
  210. end
  211. if exists(league_file || input2_file) = 0 then do
  212.    say
  213.    say al_error
  214.    say
  215.    say al_one"'"league_file||input2_file"'."
  216.    exit
  217. end
  218. if exists(league_file || input3_file) = 0 then do
  219.    say
  220.    say al_error
  221.    say
  222.    say al_one"'"league_file||input3_file"'."
  223.    exit
  224. end
  225.  
  226.  
  227. tcount = 0
  228. if open(datafile,league_file || input3_file,'r') then do
  229.    do while ~eof(datafile)
  230.       line = readln(datafile)
  231.       if pos(title,line) > 0 then        league_title = delstr(line,1,13)
  232.       if pos(points_win,line) > 0 then   ptswin=delstr(line,1,16)
  233.       if pos(points_drw,line) > 0 then   ptsdrw=delstr(line,1,16)
  234.       if pos(points_lse,line) > 0 then   ptslse=delstr(line,1,16)
  235.       if pos(points_gls,line) > 0 then   ptsgls=delstr(line,1,16)
  236.       if pos(releg,line) > 0 then        reg = delstr(line,1,12)
  237.       if pos(playother,line) > 0 then    playo = delstr(line,1,12)
  238.       if pos(promoted,line) > 0 then     promo = delstr(line,1,10)
  239.       if pos(separator,line) = 0 & line ~= "" then do
  240.          line = strip(line)
  241.          tcount          = tcount + 1
  242.          teams.tcount    = line
  243.          ah_teams.tcount = line
  244.       end
  245.    end
  246.    close(datafile)
  247. end
  248. else do
  249.    say
  250.    say al_error
  251.    say
  252.    say al_two"'"league_file || input3_file"'."
  253.    exit
  254. end
  255.  
  256. if open(datafile,league_file || input_file,'r') then do
  257.    do while ~eof(datafile)
  258.       line = readln(datafile)
  259.       if pos(team,line) > 0 then
  260.          teams_ctr = teams_ctr + 1
  261.       if pos(played,line) > 0 then  m_ply.teams_ctr  = delstr(line,1,5)
  262.       if pos(won,line) > 0 then     m_won.teams_ctr  = delstr(line,1,5)
  263.       if pos(drawn,line) > 0 then   m_drw.teams_ctr  = delstr(line,1,5)
  264.       if pos(lost,line) > 0 then    m_lost.teams_ctr = delstr(line,1,5)
  265.       if pos(goalsf,line) > 0 then  m_gof.teams_ctr  = delstr(line,1,5)
  266.       if pos(goalsa,line) > 0 then  m_goa.teams_ctr  = delstr(line,1,5)
  267.       if pos(points,line) > 0 then  m_pts.teams_ctr  = delstr(line,1,5)
  268.    end
  269.    close(datafile)
  270. end
  271. else do
  272.    say
  273.    say al_error
  274.    say
  275.    say al_two"'"league_file || input_file"'."
  276.    exit
  277. end
  278.  
  279. ggg = 0
  280. do i=1 to teams_ctr
  281.    if m_ply.i ~= 0 then ggg = 1
  282. end
  283. if ggg = 1 then do
  284.    do i=1 to teams_ctr
  285.       h_ply.i = m_ply.i
  286.       h_won.i = m_won.i
  287.       h_drw.i = m_drw.i
  288.       h_lost.i= m_lost.i
  289.       h_gof.i = m_gof.i
  290.       h_goa.i = m_goa.i
  291.       h_pts.i = m_pts.i
  292.    end
  293. end
  294. else do
  295.    do i=1 to teams_ctr
  296.       h_ply.i = 0
  297.       h_won.i = 0
  298.       h_drw.i = 0
  299.       h_lost.i= 0
  300.       h_gof.i = 0
  301.       h_goa.i = 0
  302.       h_pts.i = 0
  303.       a_ply.i = 0
  304.       a_won.i = 0
  305.       a_drw.i = 0
  306.       a_lost.i= 0
  307.       a_gof.i = 0
  308.       a_goa.i = 0
  309.       a_pts.i = 0
  310.    end
  311. end
  312.  
  313. if open(datafile,league_file || input2_file,'r') then do
  314.    do while ~eof(datafile)
  315.       line = readln(datafile)
  316.       if pos(separator,line) = 0 then do
  317.          if pos(not_played,line) = 0 then do
  318.             home_team = strip(substr(line,1,30))
  319.             goals_for = substr(line,32,2)
  320.             goals_aga = substr(line,37,2)
  321.             away_team = strip(substr(line,41,30))
  322.  
  323.             do i=1 to teams_ctr
  324.                if home_team == teams.i then do
  325.                   m_ply.i = m_ply.i + 1
  326.                   m_gof.i = m_gof.i + goals_for
  327.                   m_goa.i = m_goa.i + goals_aga
  328.                   h_ply.i = h_ply.i + 1
  329.                   h_gof.i = h_gof.i + goals_for
  330.                   h_goa.i = h_goa.i + goals_aga
  331.                   if goals_for = goals_aga then do
  332.                      m_drw.i = m_drw.i + 1
  333.                      m_pts.i = m_pts.i + ptsdrw
  334.                      h_drw.i = h_drw.i + 1
  335.                      h_pts.i = h_pts.i + ptsdrw
  336.                   end
  337.                   if goals_for < goals_aga then do
  338.                      m_lost.i= m_lost.i + 1
  339.                      m_pts.i = m_pts.i + ptslse
  340.                      h_lost.i= h_lost.i + 1
  341.                      h_pts.i = h_pts.i + ptslse
  342.                   end
  343.                   if goals_for > goals_aga then do
  344.                      m_won.i = m_won.i + 1
  345.                      m_pts.i = m_pts.i + ptswin
  346.                      h_won.i = h_won.i + 1
  347.                      h_pts.i = h_pts.i + ptswin
  348.                   end
  349.                   m_pts.i = m_pts.i + (goals_for * ptsgls)
  350.                end
  351.             end
  352.             do i=1 to teams_ctr
  353.                if away_team == teams.i then do
  354.                   m_ply.i = m_ply.i + 1
  355.                   m_gof.i = m_gof.i + goals_aga
  356.                   m_goa.i = m_goa.i + goals_for
  357.                   a_ply.i = a_ply.i + 1
  358.                   a_gof.i = a_gof.i + goals_aga
  359.                   a_goa.i = a_goa.i + goals_for
  360.                   if goals_for = goals_aga then do
  361.                      m_drw.i = m_drw.i + 1
  362.                      m_pts.i = m_pts.i + ptsdrw
  363.                      a_drw.i = a_drw.i + 1
  364.                      a_pts.i = a_pts.i + ptsdrw
  365.                   end
  366.                   if goals_for < goals_aga then do
  367.                      m_won.i = m_won.i + 1
  368.                      m_pts.i = m_pts.i + ptswin
  369.                      a_won.i = a_won.i + 1
  370.                      a_pts.i = a_pts.i + ptswin
  371.                   end
  372.                   if goals_for > goals_aga then do
  373.                      m_lost.i= m_lost.i + 1
  374.                      m_pts.i = m_pts.i + ptslse
  375.                      a_lost.i= a_lost.i + 1
  376.                      a_pts.i = a_pts.i + ptslse
  377.                   end
  378.                   m_pts.i = m_pts.i + (goals_aga * ptsgls)
  379.                end
  380.             end
  381.          end
  382.       end
  383.    end
  384.    close(datafile)
  385. end
  386. else do
  387.    say
  388.    say al_error
  389.    say
  390.    say al_two"'"league_file || input2_file"'."
  391.    exit
  392. end
  393.  
  394. if open(outfile,output_file,"w") then do
  395.    do i=1 to teams_ctr
  396.       line = left(teams.i,30,' ')
  397.       mp = right(m_ply.i,3)
  398.       mw = right(m_won.i,3)
  399.       md = right(m_drw.i,3)
  400.       ml = right(m_lost.i,3)
  401.       mgf = right(m_gof.i,5)
  402.       mga = right(m_goa.i,5)
  403.       mpts = right(m_pts.i,7)
  404.       writech(outfile,line"   "mp" "mw" "md" "ml" "mgf" "mga"  "mpts"      ")
  405.       itemp=mgf-mga
  406.       if itemp>0 then
  407.          itemp=insert("+",itemp,0,1)
  408.       itemp=right(itemp,4)
  409.       writeln(outfile,itemp)
  410.    end
  411.    close(outfile)
  412. end
  413. else do
  414.    say
  415.    say al_error
  416.    say
  417.    say al_three"'"output_file"'."
  418.    exit
  419. end
  420.  
  421. address command 'Exec/footsort '
  422.  
  423. if open(datafile2,output_file,'r') then do
  424.    do i=1 to tcount
  425.       line = readln(datafile2)
  426.       teams.i  = line
  427.       teams2.i = line
  428.    end
  429.    close(datafile2)
  430. end
  431. else do
  432.    say
  433.    say al_error
  434.    say
  435.    say al_four"'"output_file"'."
  436.    exit
  437. end
  438.  
  439. ctr = 0
  440. do while swapctr > 0
  441.    swapctr=9
  442.    do i=1 to tcount-1                               /* was 64,4 and 77,4 */
  443.       swapdone=0
  444.       j = i + 1
  445.       if strip(substr(teams.i,64,7)) = strip(substr(teams.j,64,7)) then do
  446.          goaldiffa = strip(substr(teams.i,77,5))
  447.          goaldiffb = strip(substr(teams.j,77,5))
  448.          if goaldiffa < goaldiffb then do
  449.             teams.i = teams2.j
  450.             teams.j = teams2.i
  451.             teams2.i= teams.i
  452.             teams2.j= teams.j
  453.             swapdone= 1
  454.             ctr = ctr + 1
  455.          end
  456.       end
  457.       if swapdone = 1 then
  458.          break
  459.    end
  460.    if ctr = 0 then
  461.       swapctr = 0
  462.    else
  463.       ctr = 0
  464. end
  465.  
  466. posreg   = 0
  467. pospro   = 0
  468. if reg > 0 then do             /* this bit of code checks to see which teams are relegated */
  469.    wherereg = tcount - reg
  470.    regpts   = strip(substr(teams.wherereg,64,7))         /* was 64,4 */
  471.    do i=tcount to 1 by -1
  472.       b = (((tcount-1) * playo) - strip(substr(teams.i,35,3))) * ptswin  /* was 36,2 */
  473.       c = strip(substr(teams.i,64,7)) + b
  474.       if c < regpts then posreg = i                 /* calculates the max points avail */
  475.       if c = regpts & strip(substr(teams.i,35,3)) = ((tcount-1) * playo) then
  476.          posreg = i
  477.    end
  478.    if posreg < (wherereg+1) & posreg ~= 0 then      /* was 0 - set to reqd pos if higher */
  479.       posreg = wherereg + 1
  480.                                                     /* Bug fix 0305 - relegation bar problems */
  481. end
  482.  
  483. /* ---------------------------------------------- */
  484.  
  485.  
  486. if promo > 0 then do          /* this bit of code checks to see which teams are promoted */
  487.    wherepro = promo + 1
  488.    propts   = strip(substr(teams.wherepro,64,7))         /* was 64,4 */
  489.    do i=1 to tcount
  490.       if strip(substr(teams.i,35,3)) > 0 then
  491.          b = (((tcount-1) * playo) - strip(substr(teams.i,35,3))) * ptswin
  492.       else
  493.          b = 0
  494.       c = strip(substr(teams.i,64,7)) + b
  495.       if c > propts then pospro = i                 /* calculates the max points avail */
  496.    end
  497.    if pospro > (wherepro-1) & pospro ~= 0 then
  498.       pospro = wherepro - 1                         /* was 0 - set to reqd pos if lower */
  499. end
  500.  
  501. /* ---------------------------------------------- */
  502.  
  503. lengtt = 0
  504. do i=1 to tcount
  505.    if length(ah_teams.i) > lengtt then
  506.       lengtt = length(ah_teams.i)
  507. end
  508. if lengtt > 30 then
  509.    lengtt = 30
  510.  
  511. tabtitle = al_five""left(" ",lengtt+3,' ')""al_six
  512. tabunder = left("-",length(tabtitle)+4,'-')
  513.  
  514. say
  515. say center(league_title,88)
  516. say tabunder
  517. say
  518. if ggg = 1 then do
  519.    say al_seven
  520.    say al_eight
  521. end
  522. say
  523. say al_nine""left("Team",lengtt+3,' ')""al_ten
  524. say
  525. say tabtitle
  526. say tabunder
  527. say
  528.  
  529. do i=1 to tcount
  530.    team1 = strip(substr(teams.i,1,30))
  531.    do j=1 to tcount
  532.       if pos(team1,ah_teams.j) > 0 then do
  533.          h1 = right(h_ply.j,4,' ')
  534.          h2 = right(h_won.j,4,' ')
  535.          h3 = right(h_drw.j,4,' ')
  536.          h4 = right(h_lost.j,4,' ')
  537.          h5 = right(h_gof.j,6,' ')
  538.          h6 = right(h_goa.j,6,' ')
  539.          a1 = right(a_ply.j,4,' ')
  540.          a2 = right(a_won.j,4,' ')
  541.          a3 = right(a_drw.j,4,' ')
  542.          a4 = right(a_lost.j,4,' ')
  543.          a5 = right(a_gof.j,6,' ')
  544.          a6 = right(a_goa.j,6,' ')
  545.          gdif = substr(teams.i,76,5)
  546.          if strip(gdif) = 0 then
  547.             gdif = " "
  548.          rocky = h1||h2||h3||h4||h5||h6||"  ("right(i,2,' ')") "left(ah_teams.j,lengtt+3)||a1||a2||a3||a4||a5||a6" "right(strip(substr(teams.i,64,7)),7,' ')"   "gdif
  549.       end
  550.    end
  551.    if i >= posreg & posreg~=0 then rocky = upper(rocky)
  552.    if i = 1 then rocky = upper(rocky)
  553.    say rocky
  554.    if i = (tcount-reg) & reg > 0 & posreg~=0 then do       /* was posreg */
  555.       say left("_",length(tabunder),'_')
  556.       say
  557.    end
  558.    if i = (promo) & promo > 0 & pospro~=0 then do        /* was pospro */
  559.       say left("_",length(tabunder),'_')
  560.       say
  561.    end
  562. end
  563.  
  564. say
  565. say tabunder
  566. say
  567.  
  568. exit